﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Threading;

namespace Infragistics.Composite.Wpf.Samples.ClockModule
{
    public class ClockViewModel : INotifyPropertyChanged
    {
        readonly DispatcherTimer _timer;

        public ClockViewModel()
        {
            _timer = new DispatcherTimer(DispatcherPriority.Normal);
            _timer.Interval = TimeSpan.FromSeconds(1);
            _timer.Tick += delegate { this.OnPropertyChanged("CurrentTime"); };
        }

        public string CurrentTime
        {
            get { return DateTime.Now.ToLongTimeString(); }
        }

        public void Start()
        {
            if (!this.IsRunning)
            {
                this.OnPropertyChanged("CurrentTime");
                _timer.Start();
                Debug.WriteLine("Clock started.");
            }
        }

        public void Stop()
        {
            if (this.IsRunning)
            {
                _timer.Stop();
                Debug.WriteLine("Clock stopped.");
            }
        }

        bool IsRunning
        {
            get { return _timer.IsEnabled; }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        void OnPropertyChanged(string propertyName)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion // INotifyPropertyChanged Members
    }
}